﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallJob.Model;
using SmallJob.Data;
using SmallJob.Services.Interfaces;
using SmallJob.Data.Interfaces;
using SmallJob.Services.Code;

namespace SmallJob.Services.Implements
{
    public class ProvideService : RepositoryBase<Provide>, IProvideService
    {
        public ProvideService(IDatabaseFactory DbFactory) : base(DbFactory) { }
        public List<Provide> GetByPage(int page, int pagesize, out int total)
        {
            var list = from r in SmallJobDB.Provides
                       orderby r.ID descending
                       select r;
            int skip = (page - 1) * pagesize;
            total = list.ToList().Count;
            return list.Skip(skip).Take(pagesize).ToList();
        }
        public List<Provide> Find(string s)
        {
            if (string.IsNullOrWhiteSpace(s))
                return SmallJobDB.Provides.Where(p => !p.IsDeleted).OrderByDescending(p => p.CreatedDate).ToList();
            List<Provide> result = new List<Provide>();
            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (var item in SmallJobDB.Provides.Where(p => !p.IsDeleted))
            {
                int i = StringHanding.CountContain(s, item.Title);
                if (i > 0)
                {
                    dic.Add(item.ID, i);
                }
            }
            var sorted = dic.OrderByDescending(pair => pair.Value);
            foreach (var item in sorted)
            {
                result.Add(GetById(item.Key));
            }
            return result;
        }
        public List<Provide> GetProvideByUserID(int id)
        {
            var list = from p in SmallJobDB.Provides
                       where p.UserID == id && !p.IsDeleted
                       select p;
            return list.ToList();
        }
        public List<Provide> GetProvideByCategoryID(int id)
        {
            var list = from p in SmallJobDB.Provides
                       where p.CategoryID == id && !p.IsDeleted
                       select p;
            return list.ToList();
        }
        public List<Provide> GetLastestProvide(int count)
        {
            var list = from r in SmallJobDB.Provides
                       where !r.IsDeleted
                       orderby r.CreatedDate descending
                       select r;
            return list.Take(count).ToList();
        }
        public List<Provide> GetMostPopularProvide(int count)
        {
            var list = from r in SmallJobDB.Provides
                       where !r.IsDeleted
                       orderby r.CreatedDate descending
                       select r;
            return list.Take(count).ToList();
        }
        public List<Provide> GetBuyUserID(int id)
        {
            var list = from r in SmallJobDB.Provides
                       where r.UserID == id && !r.IsDeleted
                       orderby r.CreatedDate descending
                       select r;
            return list.ToList();
        }

        public List<Provide> GetByLocation(string location)
        {
            var list = from r in SmallJobDB.Provides
                       where r.Location.Equals(location) && !r.IsDeleted
                       orderby r.CreatedDate descending
                       select r;
            return list.ToList();
        }


        public List<Provide> GetByPageFilter(int page, int pagesize, out int total, string filter)
        {
            int skip = (page - 1) * pagesize;
            var list = (from d in SmallJobDB.Provides
                        where d.Title.Contains(filter) || d.Location.Contains(filter) || d.DescriptionOrder.Contains(filter)
                        orderby d.ID descending
                        select d);

            total = list.ToList().Count;
            var alist = list.ToList();
            return list.Skip(skip).Take(pagesize).ToList();
        }

        public List<Provide> GetAllProvide()
        {
            var lst = (from p in SmallJobDB.Provides
                       where !p.IsDeleted
                       orderby p.CreatedDate descending
                       select p).ToList();
            return lst;
        }

        public List<Provide> Filter(string location, int category, string title, int minprice, int maxprice)
        {
            bool excludemin = (minprice == -1);
            bool excludemax = (maxprice == -1);
            bool cklocation = string.IsNullOrWhiteSpace(location);
            bool cktitle = string.IsNullOrWhiteSpace(title);
            var list = from r in SmallJobDB.Provides
                       where (cklocation || r.Location.Contains(location)) &&
                       (category == 0 || r.CategoryID == category) &&
                       (cktitle || r.Title.Contains(title)) &&
                        !r.IsDeleted &&
                        (excludemin ? (excludemax ? true : r.Price >= maxprice) : (excludemax ? r.Price <= minprice : (r.Price >= minprice && r.Price <= maxprice)))
                       orderby r.CreatedDate descending
                       select r;
            return list.ToList();
        }
    }
}
